Skip to content

Add expt#26

Merged
kou merged 2 commits intouim:mainfrom
jpellegrini:expt
Dec 1, 2025
Merged

Add expt#26
kou merged 2 commits intouim:mainfrom
jpellegrini:expt

Conversation

@jpellegrini
Copy link
Copy Markdown
Contributor

It signals an error when the exponent is negative, since sigscheme only implements integer math.

This is an iterative implementation of exponentiation by squaring - and it works with negative bases.

base = SCM_INT_VALUE(_base);
expo = SCM_INT_VALUE(_expo);

if (expo < 0) ERR(ERRMSG_NEGATIVE_EXP);
Copy link
Copy Markdown
Member

@kou kou Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment why we return an error here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done!

src/number.c Outdated
Comment on lines +438 to +441
if (expo % 2 == 1)
result *= base;
base *= base;
expo /= 2;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use spaces instead of tabs for indent?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, fixed!

It signals an error when the exponent is negative, since
sigscheme only implements integer math.
@jpellegrini
Copy link
Copy Markdown
Contributor Author

Tests fail with

Error: in expt: negative exponent
FAIL test-number-arith.scm (exit status: 1)

However, I did

(assert-error  (tn)     (expt  3 -2))

which I suppose would work (?)

Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
@kou
Copy link
Copy Markdown
Member

kou commented Dec 1, 2025

Thanks.

@kou kou merged commit f7305a7 into uim:main Dec 1, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants